package com.x_memteor.base_master.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CircleView extends View {
    private static final int BG_COLOR = Color.rgb(34, 138, 253);
    private static final int STEP = 5;
    private int countDown = 0;
    private Handler handler;
    private int mAngleEnd;
    private int mAngleStart;
    private Paint mPaint;
    private RectF rectF;

    public CircleView(Context context) {
        super(context);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        init();
    }

    private void init() {
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(true);
        this.rectF = new RectF(0.0f, 0.0f, (float) getWidth(), (float) getHeight());
        this.handler = new Handler();
        runLoop();
    }

    private void runLoop() {
        this.handler.postDelayed(new Runnable() {
            public void run() {
                CircleView.this.countDown = CircleView.this.countDown + 5;
                if (CircleView.this.countDown < 360) {
                    CircleView.this.mAngleEnd = CircleView.this.mAngleEnd + 5;
                } else if (CircleView.this.countDown < 720) {
                    CircleView.this.mAngleStart = CircleView.this.mAngleStart + 5;
                    CircleView.this.mAngleEnd = CircleView.this.mAngleEnd - 5;
                } else {
                    CircleView.this.mAngleStart = CircleView.this.mAngleEnd = 0;
                    CircleView.this.countDown = 0;
                }
                CircleView.this.invalidate();
                CircleView.this.runLoop();
            }
        }, 150);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.mPaint.setColor(BG_COLOR);
        canvas.drawArc(this.rectF, (float) this.mAngleStart, (float) this.mAngleEnd, true, this.mPaint);
    }
}